package com.example.AdminComponent;

import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.ParseStudent;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;


public class Student extends Box {

    JTable Table;
    Vector<String> student=new Vector<>();
    Vector<Vector<String>> students=new Vector<Vector<String>>();
    TableModel tm;


    //构造方法
    public Student(){
        super(BoxLayout.Y_AXIS);
        //组装
        JPanel jPanel=new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(700,50));
        //jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JButton b1=new JButton("修改");
        JButton b2=new JButton("删除");
        JButton b3=new JButton("查询");
        JLabel name=new JLabel("学号");
        JLabel aclass=new JLabel("班级");
        JTextField t1=new JTextField(6);
        JTextField t2=new JTextField(6);
        Box box1= Box.createHorizontalBox();
        jPanel.add(name);
        jPanel.add(t1);
        jPanel.add(aclass);
        jPanel.add(t2);
        jPanel.add(b3);
        jPanel.add(Box.createHorizontalStrut(50));
        jPanel.add(b1);
        jPanel.add(b2);
        this.add(jPanel);

        //组装表格
       String []ss={"学号","姓名","班级","课程","成绩"};
        for (int i = 0; i < ss.length; i++) {
            student.add(ss[i]);
        }
        requestPostData("http://localhost:8080/student");
        tm=new DefaultTableModel(students,student);
        Table=new JTable(tm){
            @Override
            public boolean isCellEditable(int row,int column){
                return false;
            }
        };

        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        Table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        JScrollPane scrollPane=new JScrollPane(Table);
        Table.setFont(new Font("宋体",Font.PLAIN,15));
        this.add(scrollPane);
        //查询按钮绑定
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (t2.getText().trim().equals("")&&!t1.getText().trim().equals("")) {
                    requestGetData("http://localhost:8080/select" + "?id=" + t1.getText());
                }else
                if (t1.getText().trim().equals("")&&!t2.getText().trim().equals("")) {
                    requestGetData("http://localhost:8080/select" + "?aaClass=" + t2.getText());
                } else  if (!t1.getText().trim().equals("")&&!t2.getText().trim().equals("")){
                    requestGetData("http://localhost:8080/select" + "?id=" + t1.getText() + "&aaClass=" + t2.getText());
                }else {
                    requestPostData("http://localhost:8080/student");
                }
                //刷新表格
                Table.updateUI();
            }
        });


        /*
        * 修改按钮
        * */
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = Table.getSelectedRow();//获取行
                //   DefaultTableModel dm= (DefaultTableModel) Table.getModel();//获取内容模型
                //弹出面板
                JFrame update = new UpdateJFrameStudent((String) tm.getValueAt(row, 0), (String) tm.getValueAt(row, 1),
                        (String) tm.getValueAt(row, 2), (String) tm.getValueAt(row, 3), (String) tm.getValueAt(row, 4),Table);


            }
        });



        /*
        * 删除按钮
        *
        * */
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = Table.getSelectedRow();//获取行
                DefaultTableModel dm= (DefaultTableModel) tm;
               String id=(String) tm.getValueAt(row, 0);
                dm.removeRow(row);
                HttpUtil.sendGet("http://localhost:8080/student/delete/"+id);
                JOptionPane.showMessageDialog(null,"删除成功" );
            }
        });


    }

    //请求数据
    public  void requestPostData( String url){
        String result=HttpUtil.sendPost(url);
        Vector<ParseStudent> parseStudents= ResultUtil.ResultVectorStudent(result);
        Vector<Vector<String>> ss=new Vector<>();
        for (int i = 0; i < parseStudents.size(); i++) {
            Vector<String> s=new Vector<>();
            s.add(parseStudents.get(i).getId());
            s.add(parseStudents.get(i).getName());
            s.add(parseStudents.get(i).getAaClass());
            s.add(parseStudents.get(i).getCourse());
            s.add(String.valueOf(parseStudents.get(i).getScore()));
            ss.add(s);
        }

        students.clear();
        for (Vector<String> sv  : ss) {
            students.add(sv);
        }
    }



    public  void requestGetData( String url) {
        String result = HttpUtil.sendGet(url);
        Vector<ParseStudent> parseStudents = ResultUtil.ResultVectorStudent(result);
        Vector<Vector<String>> ss = new Vector<>();
        for (int i = 0; i < parseStudents.size(); i++) {
            Vector<String> s = new Vector<>();
            s.add(parseStudents.get(i).getId());
            s.add(parseStudents.get(i).getName());
            s.add(parseStudents.get(i).getAaClass());
            s.add(parseStudents.get(i).getCourse());
            s.add(String.valueOf(parseStudents.get(i).getScore()));
            ss.add(s);
        }
        students.clear();
        for (Vector<String> sv  : ss) {
            students.add(sv);
        }

    }



}
